package com.wanyang.controller;

import com.wanyang.dto.PageDTO;
import com.wanyang.dto.ProblemFeedbackDTO;
import com.wanyang.service.MemberService;
import com.wanyang.service.MemberSignService;
import com.wanyang.service.ProblemFeedbackService;
import com.wanyang.service.UserContext;
import com.wanyang.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags="我的")
@RestController
@RequestMapping("mine")
public class MineController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberSignService memberSignService;

    @Autowired
    private ProblemFeedbackService problemFeedbackService;

    @ApiOperation(value = "查询我的信息")
    @GetMapping("info")
    public ApiResult info() {

        return ApiResult.success(memberService.getVO(UserContext.get().getId()), null);
    }

    @Data
    public static class UpdateInfoDto {
        String key;
        String value;
    }

    @ApiOperation(value = "更新自己的信息, 支持的key {\"nickname\", \"personalizedSignature\", \"sex\", \"avatar\", \"backgroundImg\"}")
    @PostMapping("updateInfo")
    public ApiResult updateInfo(@RequestBody UpdateInfoDto updateInfoDto) {

        return ApiResult.success(memberService.updateInfo(UserContext.get().getId(), updateInfoDto.key, updateInfoDto.value), null);
    }

    @ApiOperation(value = "查询本周签到记录")
    @GetMapping("thisWeekSignRecord")
    public ApiResult thisWeekSignRecord() {

        return ApiResult.success(memberSignService.thisWeekSignRecord(UserContext.get().getId()), null);
    }

    @ApiOperation(value = "签到", notes = "signedDate 格式，例如20201010")
    @PostMapping("signed/{signedDate}")
    public ApiResult signed(@PathVariable String signedDate) {
        memberSignService.signed(UserContext.get().getId(), signedDate);
        return ApiResult.success();
    }

    @ApiOperation(value = "金币历史", notes = "signedDate 格式，例如20201010")
    @GetMapping("goldRecord")
    public ApiResult goldRecord(PageDTO pageDTO) {

        return ApiResult.success(memberService.getGoldRecord(UserContext.get().getId(), pageDTO), null);
    }

    @ApiOperation(value = "问题反馈", notes = "problemType: 1-问题，2-建议")
    @PostMapping("problemFeedback")
    public ApiResult problemFeedback(@RequestBody ProblemFeedbackDTO problemFeedbackDTO) {
        problemFeedbackService.submit(problemFeedbackDTO);
        return ApiResult.success();
    }
}
